<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="assets/layui-v2.4.5/layui/css/layui.css" />
		<!--<script src="vendor/jquery/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>-->
		<script src="assets/layui-v2.4.5/layui/layui.js"></script>

		<link rel="stylesheet" type="text/css" href="js/bootstarp-3.3.7/css/bootstrap.min.css" />
		<script src="js/jquery/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/bootstarp-3.3.7/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>

		<link rel="stylesheet" type="text/css" href="js/bootstarp-table-1.11.1/bootstrap-table.min.css" />
		<script src="js/bootstarp-table-1.11.1/bootstrap-table.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/bootstarp-table-1.11.1/bootstrap-table-zh-CN.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.f {
				font-family: "微软雅黑";
				font-size: 16px;
			}
			
			.f2 {
				font-family: "微软雅黑";
				font-size: 18px;
				font-weight: bold;
				color: silver;
			}
			
			#p1,
			#p2 {
				font-family: "微软雅黑";
				font-size: 16px;
				font-weight: bold;
			}
			
			.s {
				width: 120px;
			}
		</style>

		<script>
			$(function() { //, headers: { 'x-requested-with': 'XMLHttpRequest' }
				$.ajaxSetup({
					crossDomain: true,
					xhrFields: {
						withCredentials: true
					}
				});
			});
		</script>

		<script>
			var isNewPatient = 0;

			$(function() {
				$("#operatorId").val(1)
				//					var getSelectRows = $("#doctorTab").bootstrapTable('getSelections', function(row) {
				//						return row;
				//					});
				//					
				//					alert(getSelectRows.true_name)

				$("#username").blur(function() {

					//					alert()
					if($("#username").val().length != 0) {

						$.ajax({
							type: "get",
							url: "http://127.0.0.1:8099/mzgh/findByName",
							dataType: "json",
							data: "name=" + $("#username").val(),
							success: function(data) {
								layui.use(['layer', 'form'], function() {
									var layer = layui.layer,
										form = layui.form;
									if(data.patient != null) {
										isNewPatient = 0;
										//										alert(1)
										layer.msg("老患者：" + data.patient.name)
										if(data.patient.sex == "男") {
											$("#man").attr("disabled", false)
											$("#woman").attr("disabled", true)
										} else {
											$("#man").attr("disabled", true)
											$("#woman").attr("disabled", false)
										}
										form.val('patientMsg', {
											"username": data.patient.name,
											"sex": data.patient.sex,
											"birthday": data.patient.birthday,
											"age": data.patient.age + "周岁",
											"phone": data.patient.mobile,
											"address": data.patient.address,
											"idCard": data.patient.id_card
											//											"desc": "我爱 layui"
										})

									} else {
										isNewPatient = 1;
										//										alert(2)
										layer.msg("新患者")
										//										alert(3)
										$("#man").attr("disabled", false)
										$("#woman").attr("disabled", false)
										form.val('patientMsg', {
											"sex": "女",
											"birthday": "",
											"age": "",
											"phone": "",
											"address": "",
											"idCard": ""
											//											"desc": "我爱 layui"
										})
										getNew()

									}
								})

							}

						});
					}

				})

				$("#doctotTab").bootstrapTable({
					url: "http://127.0.0.1:8099/mzgh/findRegistration",
					pagination: true,
					pageSize: 6,
					singleSelect: true,
					//						queryParams: queryParams2,
					clickToSelect: true,
					onClickRow: function(row, tr, flied) {

						$("#id").val(row.registeredfee_aggregate_amount)
						$("#create_time").val(row.create_time)
						$("#money").html((row.registeredfee_aggregate_amount).toFixed(2))  
						$("#physicianId").val(row.id)
						$("#departmentId").val(row.did)
						$("#registeredfeeId").val(row.reg_id)
						$("#schedulingId").val(row.scheduling_id)
						$("#schedulingSubsectionId").val(row.scheduling_subsection_id)
						$("#schedulingWeekId").val(row.scheduling_week_id)

					},
					queryParams: query,
					columns: [{
						radio: true,
						width: 80,
					}, {
						field: 'create_time',
						title: '日期',
						align: 'center',
					}, {
						field: 'stype',
						title: '时间段',
						align: 'center',
					}, {
						field: 'department_name',
						title: '科室名称',
						align: 'center',
					}, {
						field: 'true_name',
						title: '医生姓名',
						align: 'center',
					}, {
						field: 'registeredfee_name',
						title: '挂号类型',
						align: 'center',
					}, {
						field: 'registeredfee_aggregate_amount',
						title: '挂号费(元)',
						align: 'center',
					}]
				})

				$.ajax({
					type: "get",
					url: "http://127.0.0.1:8099/mzgh/findDoctor",
					dataType: "json",
					success: function(data) {
						for(var i = 0; i < data.doctor.length; i++) {
							//							alert(data.doctor[i].id)
							$("#doc").append("<option value='" + data.doctor[i].id + "'>" + data.doctor[i].true_name + "</option>")
						}

						for(var i = 0; i < data.regi.length; i++) {
							$("#regi").append("<option value='" + data.regi[i].reg_id + "'>" + data.regi[i].registeredfee_name + "</option>")
						}

						for(var i = 0; i < data.department.length; i++) {
							$("#department").append("<option value='" + data.department[i].did + "'>" + data.department[i].department_name + "</option>")
						}

					}
				});
				layui.use(['layer', 'form', 'laydate', 'element', 'table'], function() {
					var layer = layui.layer,
						form = layui.form,
						layedit = layui.layedit,
						laydate = layui.laydate,
						table = layui.table,
						element = layui.element;

					laydate.render({

						elem: '#birthday',
						done: function(value, date) {
							var d = new Date();
							var b = new Date(value)

							if(d.getTime() < b.getTime()) {
								layer.alert("生日不能大于当前日期")
								$("#birthday").val("")
								$("#age").val("")

							} else {

								$("#age").val(getAge(value))
							}
						}
					});

					laydate.render({
						elem: '#ghdate'

					})
					form.verify({
						did: function(value, item) {
							if(value.length == 0) {
								return "请选择挂号信息";
							}

						}

					})

					form.on('submit(tj)', function(data) {

						$.ajax({
							type: "post",
							url: "http://127.0.0.1:8099/mzgh/addPatient",
							dataType: "json",
							data: $("#form2").serialize() + "&" + $("#form").serialize() + "&isNewPatient=" + isNewPatient,
							success: function(data) {

								if(data == 1) {
									layer.msg('挂号成功', {
										icon: 1,
										time: 1500,
										end: function() {
											location.reload()
										}
									});
								}
							}
						});

					})

				});

			})

			function getNew() {
				$("#idCard").blur(function() {
					if($("#idCard").val().length != 0) {
						$.ajax({
							type: "get",
							url: "http://127.0.0.1:8099/mzgh/findByIdcard",

							dataType: "json",
							data: "id_card=" + $("#idCard").val(),
							success: function(data) {
								if(data.patientBy != null) {

									layer.msg("身份证号码重复", {

										icon: 5,
										anim: 6,
										time: 1000
									})
									$("#idCard").val("")
								}

							}
						});
					}

				})
			}

			function getAge(birthday) {
				var mydate = new Date();
				var mydateMounth = mydate.getMonth();
				var bir = new Date(birthday)
				var birMounth = bir.getMonth();
				//算年龄
				var sumMydateMounth = mydate.getFullYear() * 12 + (mydateMounth + 1)
				var sumBirMounth = bir.getFullYear() * 12 + (birMounth + 1)
				//算年龄
				var m = (sumMydateMounth - sumBirMounth) / 12
				//算周岁零几个月
				var yushu = (sumMydateMounth - sumBirMounth) % 12

				if(m < 1) {
					if(yushu < 1) {
						return "未满月";
					}
					return yushu + "个月";
				} else if(yushu < 1) {
					return m + "周岁";
				} else {

					return parseInt(m) + "周岁" + parseInt(yushu) + "个月";
				}
			}

			function showPatient() {

				layer.open({
					type: 2 //Page层类型
						,
					area: ['800px', '600px'],
					title: '选择患者',
					shade: 0.6 //遮罩透明度
						,
					maxmin: true //允许全屏最小化
						,
					anim: 1 //0-6的动画形式，-1不开启
						,
					content: 'zhd/hzk.html',
					end: function() {

						if($("#patientId").val().length == 0) {
							layer.msg("无信息")
						} else {
							$.ajax({
								type: "get",
								url: "http://127.0.0.1:8099/nw/selectOnePatient",
								async: true,
								dataType: "json",
								data: "pid=" + $("#patientId").val(),
								success: function(data) {

									isNewPatient = 0;
									//									alert(isNewPatient)
									layui.use(['layer', 'form'], function() {
										var layer = layui.layer,
											form = layui.form;
										if(data.patient.sex == "男") {
											$("#man").attr("disabled", false)
											$("#woman").attr("disabled", true)
										} else {
											$("#man").attr("disabled", true)
											$("#woman").attr("disabled", false)
										}
										form.val('patientMsg', {
											"username": data.patient.name,
											"sex": data.patient.sex,
											"birthday": data.patient.birthday,
											"age": data.patient.age + "周岁",
											"phone": data.patient.mobile,
											"address": data.patient.address,
											"idCard": data.patient.id_card
											//											"desc": "我爱 layui"
										})

									})
								}
							});

						}

					}

				});

			}

			//			function timestampToTime(timestamp) {
			//				var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			//				var Y = date.getFullYear() + '-';
			//				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
			//				var D = date.getDate() + ' ';
			//				var h = date.getHours() + ':';
			//				var m = date.getMinutes() ;
			//				if (h<10){
			//					h = "0"+h;
			//				}else if(m<10){
			//					m = "0"+m;
			//				}
			//				
			//				return Y + M + D + h + m ;
			//			}

			function refresh() {
				$("#doctotTab").bootstrapTable("refresh")
			}

			function query() {

				return {
					'did': $("#department").val(),
					'id': $("#doc").val(),
					'reg_id': $("#regi").val(),
					'create_time': $("#ghdate").val(),
					'subsection_type': $("#time").val()

				}
			}
		</script>

		<script type="text/html" id="dell">
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>
	</head>

	<body style="background-color: #F3F5F7;">
		<form id="form2">

			<input type="hidden" name="create_time" id="create_time" value="" />
			<input type="hidden" name="operatorId" id="operatorId" value="" />
			<input type="hidden" name="physicianId" id="physicianId" value="" />
			<input type="hidden" name="departmentId" id="departmentId" value="" />
			<input type="hidden" name="registeredfeeId" id="registeredfeeId" value="" />
			<input type="hidden" name="schedulingId" id="schedulingId" value="" />
			<input type="hidden" name="schedulingSubsectionId" id="schedulingSubsectionId" value="" />
			<input type="hidden" name="schedulingWeekId" id="schedulingWeekId" value="" />
			<input type="hidden" name="patientId" id="patientId" value="" />
		</form>

		<div style="padding: 20px;">

			<div class="layui-row layui-col-space10">

				<!--患者信息-->
				<div class="layui-col-md5">
					<div class="layui-card" style="background-color:#F9F9F9; height: 600px;">

						<div class="layui-card-header" style="font-family: '微软雅黑'; font-size: 20px; font-weight:bold;">患者信息<button style="margin-left: 50px;" type="button" class="layui-icon layui-icon-friends layui-btn layui-btn-primary layui-btn-radius" onclick="showPatient()"> 选择患者</button></div>
						<div class="layui-card-body">
							<form id="form" class="layui-form layui-form-pane" lay-filter="patientMsg">
								<input type="hidden" name="registration_amount" id="id" value="" lay-verify="did" />
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label f">患者姓名</label>
										<div class="layui-input-inline">
											<input type="text" name="username" id="username" lay-verify="username|required" autocomplete="off" placeholder="请输入患者姓名(必填)" class="layui-input">
										</div>

									</div>
									<div class="layui-inline">
										<label class="layui-form-label f">性别</label>
										<div class="layui-input-inline">
											<input type="radio" id="man" name="sex" value="男" title="男">
											<input type="radio" id="woman" name="sex" value="女" title="女" checked="checked">
										</div>
									</div>

								</div>

								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label f">生日</label>
										<div class="layui-input-inline">
											<input type="text" name="birthday" lay-verify="required" id="birthday" placeholder="请输入患者生日(必填)" class="layui-input">
										</div>
									</div>

									<div class="layui-inline">
										<label class="layui-form-label f">年龄</label>
										<div class="layui-input-inline">
											<input type="text" name="age" id="age" disabled="disabled" value="" autocomplete="off" placeholder="年龄自动生成" class="layui-input">
										</div>
									</div>

								</div>
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label f">手机号</label>
										<div class="layui-input-inline">
											<input type="text" name="phone" lay-verify="required|phone" autocomplete="off" placeholder="请输入患者手机号码(必填)" class="layui-input">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label f">身份证</label>
										<div class="layui-input-inline" style="width: 250px;">
											<input type="text" id="idCard" name="idCard" autocomplete="off" lay-verify="required|identity" placeholder="请输入18位患者身份证号码(必填)" class="layui-input">
										</div>
									</div>

								</div>

								<div class="layui-form-item layui-form-text">
									<label class="layui-form-label f">患者地址</label>
									<div class="layui-input-block">
										<textarea placeholder="请输入患者的住址信息(选填,限100字)" name="address" class="layui-textarea"></textarea>
									</div>
								</div>

								<div class="layui-form-item" align="center">
									<div class="layui-inline">

										<div class="layui-input-inline">
											<button type="button" lay-submit="" lay-filter="tj" class="layui-btn layui-btn-danger layui-btn-radius"><i class="glyphicon glyphicon glyphicon-yen" style="font-size: 16px;"></i><font id="money" style="font-size: 16px;">0:00</font></button> &emsp;

										</div>

									</div>

								</div>
							</form>
						</div>
					</div>

				</div>

				<div class="layui-col-md7">
					<div class="layui-card" style="height: 600px;">

						<div class="layui-card-body" align="center">
							<div class="layui-card-header" style="text-align: left; font-family: '微软雅黑'; font-size: 20px; font-weight:bold;">选择挂号</div>
							<div class="layui-inline">
								<br />
								<form class="layui-form">
									<div class="layui-inline s">
										<div class="layui-input-inline">
											<select id="department">
												<option value="" selected="">-就诊科室-</option>

											</select>
										</div>
									</div>
									<div class="layui-inline s">
										<div class="layui-input-inline">
											<select id="doc">
												<option value="" selected="">-医生-</option>

											</select>
										</div>
									</div>

									<div class="layui-inline s">
										<div class="layui-input-inline">
											<select name="quiz1" id="regi">
												<option value="" selected="">-挂号类别-</option>

											</select>
										</div>
									</div>

									<div class="layui-inline s">
										<div class="layui-input-inline">

											<input type="text" name="date" id="ghdate" placeholder="挂号日期" autocomplete="off" class="layui-input">

										</div>
									</div>
									<div class="layui-inline s">
										<div class="layui-input-inline">
											<select id="time">
												<option value="" selected="">-时间段-</option>
												<option value="1">上午</option>
												<option value="2">下午</option>
												<option value="3">晚上</option>
											</select>
										</div>
									</div>
									<div class="layui-inline">
										<div class="layui-input-inline">
											<button type="button" onclick="refresh()" class="layui-btn layui-btn-primary layui-btn-radius glyphicon glyphicon-search"></button>
										</div>
									</div>
								</form>

							</div>
							<br />
							<br />
							<div>
								<table id="doctotTab" class=""></table>
							</div>
						</div>
					</div>
				</div>

			</div>

		</div>
		</div>

	</body>

</html>